<?php
/*
Plugin Name: Easier Responsive Slider
Version: 0.0.1
Plugin URI: 
Author: <a href="">Antonio Ibáñez</a>
Description: Slider de imágenes fácil de usar y altamente editable.
Text Domain: easy-responsive-slider
License: GPLv3 or later
Domain Path: /languages
*/

if (!defined('ABSPATH')) {
    exit;
}

require_once 'model/easier_rs_view.php';
require_once 'model/easier_rs_database.php';
require_once 'model/easier_create_slider.php';



class Easier_responsive_slider {
    var $plugin_version = '0.0.1';

    function __construct() {
        //define('EASIER_RESPONSIVE_SLIDER', $this->plugin_version);
        //register_activation_hook( __FILE__, array( $this, 'wpa_install' ) );
        //register_deactivation_hook( __FILE__, array( $this, 'wpa_uninstall' ) );
        $this->plugin_includes();
    }

    function plugin_includes() {
        if (is_admin()) {
            add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2);
        }
        add_action( 'admin_menu', array($this, 'easier_add_menu' ));
        add_action('admin_enqueue_scripts', array($this , 'admin_style_easier'));
        add_shortcode( 'easier_rs', 'easier_responsive_slider_insert' );
        //add_action('admin_enqueue_scripts', array($this , 'admin_enqueue_scripts'));
        add_action('wp_enqueue_scripts', array($this , 'easier_rs_scripts'));
        
    }

    function easier_add_menu() {
        //add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null );
        add_menu_page('Easier Responsive Slider', 'Easier Responsive Slider', 'manage_options', 'easier_responsive_slider', array($this, 'admin_easier_responsive_slider'));
        
        /*$sliders_page = add_submenu_page('sliders_wdps', __('Sliders','wdps_back'), __('Sliders','wdps_back'), 'manage_options', 'sliders_wdps', 'wdps_sliders');
        add_action('admin_print_styles-' . $sliders_page, 'wdps_styles');
        add_action('admin_print_scripts-' . $sliders_page, 'wdps_scripts');

        add_submenu_page('sliders_wdps', __('Get Pro', 'wdps_back'), __('Get Pro', 'wdps_back'), 'manage_options', 'licensing_wdps', 'wdps_licensing');
        add_submenu_page('sliders_wdps', __('Featured Plugins','wdps_back'), __('Featured Plugins','wdps_back'), 'manage_options', 'featured_plugins_wdps', 'wdps_featured');
        add_submenu_page('sliders_wdps', __('Featured Themes','wdps_back'), __('Featured Themes','wdps_back'), 'manage_options', 'featured_themes_wdps', 'wdps_featured_themes'); 
        $uninstall_page = add_submenu_page('sliders_wdps', __('Uninstall','wdps_back'), __('Uninstall','wdps_back'), 'manage_options', 'uninstall_wdps', 'wdps_sliders');
        add_action('admin_print_styles-' . $uninstall_page, 'wdps_styles');
        add_action('admin_print_scripts-' . $uninstall_page, 'wdps_scripts');*/
    }
    
    /**
     * Método para administrar el contenido que aparece dentro de la página de administración
     * de Wordpress
     */
    function admin_easier_responsive_slider() {
        //$category_id = ((isset($_POST['category_id']) && esc_html(stripslashes($_POST['category_id'])) != -1) ? esc_html(stripslashes($_POST['category_id'])) : '');
        //$category_name = 3 ? get_the_category_by_ID(3) : '';
        
        if (!isset($_POST['submit']) && !isset($_GET['add_new'])) {
            Easier_rs_view::show_main();
        }
        else if (!isset($_POST['submit'])) {
            Easier_rs_view::show_add_slider();
        }
        else {
            $db = new Easier_rs_database();
            /*
            ?>
            <div class="wrap">
                <?php
                $this->get_posts_by(5, 3);
                ?>
            </div>
             */
            //https://developer.wordpress.org/reference/functions/wp_redirect/
            //Posible uso para después
            $data = Easier_create_slider::get_options();
            $db->easier_insert_slider($data);
            
        } 
        
        
    }
    
    function admin_style_easier() {
        $plugin_url = plugins_url('', __FILE__);
        wp_enqueue_style('easier-rs-styles', plugins_url('assets/css/admin_easier_rs.css',__FILE__));
        wp_register_script( 'easier_rs_admin_script', $plugin_url . '/assets/js/admin_easier_rs.js', array());
        wp_enqueue_script('easier_rs_admin_script');
    }
    
    function easier_rs_scripts() {
        $plugin_url = plugins_url('', __FILE__);
        wp_register_script( 'easier_rs', $plugin_url . '/assets/js/easier_rs.js', array());
        wp_enqueue_script('easier_rs');
        wp_register_style('easier-rs-style', $plugin_url . '/assets/css/easier_rs.css', array());
        wp_enqueue_style('easier-rs-style');
        
    }

    function plugin_action_links($links, $file) {
        if ($file == plugin_basename(dirname(__FILE__) . '/easier-responsive-slider.php')) {
            $links[] = '<a href="options-general.php?page=easy-slider-settings">'.__('Settings', 'easy-slider').'</a>';
        }
        return $links;
    }
    
    function get_posts_image() {
        
    }
}


$GLOBALS['easier_responsive_slider'] = new Easier_responsive_slider();
register_activation_hook(__FILE__, 'easier_rs_install_hooks');

function easier_responsive_slider_insert($atts, $content=null) {
    extract(shortcode_atts(array(
        'id' => ''
    ), $atts));
    $db = new Easier_rs_database();
    $data = $db->easier_get_slider_by_id($id);
    $data_post = Easier_create_slider::get_posts_by(5, $data[0]["category"]);
    $js_array =  json_encode($data_post);
    $url = $data_post[0][0];
    $title = $data_post[0][1];
    $img = $data_post[0][2];
    $output = "<div>
                <a href='$url' title='$title' id='easier_rs_link'>
                    <img src='$img' id='easier_responsive_slider'>
                </a><br><br>
            </div>
            <script>
            var javascript_array = " . $js_array . ";\n
            easier_rs_setInfo(javascript_array);
            
            jQuery(function() {
                setInterval(easier_rs_images, 5000);
            });
            </script>";
    return $output;
}

function easier_responsive_uninstall(){
    register_uninstall_hook( __FILE__, 'easier_rs_delete_db' );
}

function easier_rs_install_hooks() {
    easier_rs_create_db();
    easier_responsive_uninstall();
}

function easier_rs_create_db() {
    require_once 'model/easier_rs_database.php';
    Easier_rs_database::easier_create_table();
}

function easier_rs_delete_db() {
    require_once 'model/easier_rs_database.php';
    Easier_rs_insert::easier_delete_table();
}